package com.dropdb.demo.controller.wx;

import com.dropdb.demo.bean.User;
import com.dropdb.demo.bean.vo.BaseRespVO;
import com.dropdb.demo.bean.vo.CouponListData;
import com.dropdb.demo.bean.vo.CouponListElementtData;
import com.dropdb.demo.service.PromotionService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("wx/coupon")
@Validated
public class WxCouponController {

    @Autowired
    PromotionService promotionService;

    @GetMapping("selectlist")
    public BaseRespVO selectlist(@Min(0) Integer cartId, @Min(0) Integer grouponRulesId) {
        //以下代码测试用
//        List<CouponListElementtData> couponListElementtData = promotionService.selectCouponList(1, cartId);
//        return null != couponListElementtData ?
//                BaseRespVO.ok(couponListElementtData)
//                : BaseRespVO.error(502, "系统内部错误");
        User user = (User) SecurityUtils.getSubject().getPrincipal(); //没登录需要非空检查
        if (null == user) {
            return BaseRespVO.error(501, "请登录");
        }
        List<CouponListElementtData> couponListElementtData = promotionService.selectCouponList(user.getId(), cartId);
        return null != couponListElementtData
                ? BaseRespVO.ok(couponListElementtData)
                : BaseRespVO.error(502, "系统内部错误");
    }

    @GetMapping("list")
    public BaseRespVO list(@Min(1) Integer page, @Min(1) Integer size) {
        User user = (User) SecurityUtils.getSubject().getPrincipal(); //没登录需要非空检查
        if (null == user) {
            return BaseRespVO.error(501, "请登录");
        }
        CouponListData couponListData = promotionService.couponList(user.getId(), page, size);
        return null != couponListData
                ? BaseRespVO.ok(couponListData)
                : BaseRespVO.error(502, "系统内部错误");
    }

    @GetMapping("mylist")
    public BaseRespVO mylist(@Min(0) @Max(2) Integer status, @Min(1) Integer page, @Min(1) Integer size) {
        User user = (User) SecurityUtils.getSubject().getPrincipal(); //没登录需要非空检查
        if (null == user) {
            return BaseRespVO.error(501, "请登录");
        }
        CouponListData couponListData = promotionService.myCouponList(user.getId(), status, page, size);
        return null != couponListData
                ? BaseRespVO.ok(couponListData)
                : BaseRespVO.error(502, "系统内部错误");
    }

    @PostMapping("exchange")
    public BaseRespVO exchange(@RequestBody Map map) {
        String code = (String) map.get("code");
        if (null == code) {
            return BaseRespVO.error(742, "优惠券不正确");
        }
        User user = (User) SecurityUtils.getSubject().getPrincipal(); //没登录需要非空检查
        if (null == user) {
            return BaseRespVO.error(501, "请登录");
        }
        int i = promotionService.exchangeCoupon(user.getId(), code);
        switch(i) {
            case -1: return BaseRespVO.error( 742, "优惠券不正确");
            case -2: return BaseRespVO.error(741, "优惠券不能兑换");
            case -3: return BaseRespVO.error(740, "亲领过优惠券了");
            case -10: return BaseRespVO.error(502, "系统内部错误");
        }
        return BaseRespVO.ok();
    }

    @PostMapping("receive")
    public BaseRespVO receive(@RequestBody Map map) {
        Integer couponId = (Integer) map.get("couponId");
        if (null == couponId) {
            return BaseRespVO.error(742, "优惠券不正确");
        }
        User user = (User) SecurityUtils.getSubject().getPrincipal(); //没登录需要非空检查
        if (null == user) {
            return BaseRespVO.error(501, "请登录");
        }
        int i = promotionService.receiveCoupon(user.getId(), couponId);
        switch(i) {
            case -1: return BaseRespVO.error( 742, "优惠券不正确");
            case -2: return BaseRespVO.error(741, "优惠券不能兑换");
            case -3: return BaseRespVO.error(740, "亲领过优惠券了");
        }
        return BaseRespVO.ok();
    }
}
